Skip to content

fix: store raw permissions so change-detection in useFetchChatData works#1318

Open
yash113gadia wants to merge 1 commit into
RocketChat:developfrom
yash113gadia:fix/permissions-cache-raw
Open

fix: store raw permissions so change-detection in useFetchChatData works#1318
yash113gadia wants to merge 1 commit into
RocketChat:developfrom
yash113gadia:fix/permissions-cache-raw

Conversation

@yash113gadia
Copy link
Copy Markdown

Closes #1317

Problem

fetchAndSetPermissions guards re-applying permissions with:

JSON.stringify(permissions) !== JSON.stringify(permissionsRef.current.raw)

…but the ref was only ever set to { map: permissionsMap }.raw is never written. So the comparison reads undefined every time and is always truthy, meaning the permission map is rebuilt and applyPermissions (six zustand setters) runs on every call, even when permissions haven't changed. The change-detection is dead code.

Fix

Store the raw permissions next to the map so the guard can actually compare:

permissionsRef.current = {
  raw: permissions,
  map: permissionsMap,
};

Now an unchanged permissions payload short-circuits and returns the cached map, avoiding redundant setter calls / re-renders. One-line, behavior-preserving for the changed-permissions path.

…very call

fetchAndSetPermissions guards its work with
`JSON.stringify(permissions) !== JSON.stringify(permissionsRef.current.raw)`,
but the ref was only ever assigned `{ map: permissionsMap }` — `.raw` was
never written. So the comparison read `undefined` every time and was always
truthy, meaning the permission map was rebuilt and applyPermissions (which
fires six zustand setters) ran on every call, even when permissions were
unchanged. The change-detection was effectively dead.

Store the raw permissions alongside the map so the guard works as intended:
unchanged permissions now short-circuit and return the cached map.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Permissions change-detection in useFetchChatData is dead — applyPermissions re-runs on every call

1 participant